<template>
  <div>
    <!-- 部门管理 -->
    <!-- <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="24">
        <el-col :span="24"> </el-col>
      </el-row>
    </el-card> -->

    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd" style="margin-bottom: 8px;">添加</el-button>

      <el-table :data="tableData" border style="width: 100%; margin-bottom: 20px" height="60vh" row-key="departId"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
          <template #default="scope">
            <div v-if="item.prop === 'disabledFlag'">
              <el-tag v-if="scope.row.disabledFlag == 0" type="success">正常</el-tag>
              <el-tag v-else-if="scope.row.disabledFlag == 1" type="danger">禁用</el-tag>
            </div>
            <div v-else>{{ scope.row[item.prop] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-edit" link size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-popconfirm class="button10Left" :title="row.disabledFlag == 0 ? '确定要禁用吗？' : '确定要启用吗？'"
              @confirm="() => handleDisabled(row)">
              <!-- <el-button slot="reference" icon="el-icon-error" link size="mini"
                :class="row.disabledFlag==0?'danger_button':''">{{row.disabledFlag==0?'禁用':'启用'}}</el-button> -->
            </el-popconfirm>
            <el-popconfirm class="button10Left" title="确定要删除吗？" @confirm="() => handleDelete(row)">
              <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <dept-dialog ref="deptDialog" @ok="loadData" />
  </div>
</template>

<script>
import { pageList, del } from '@/api/sys/dept'
import deptDialog from '@/views/Setting/dept/deptDialog.vue'

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Dept',
  components: { deptDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {},
      tableProps: [
        { title: '部门名称', prop: 'departName', width: '', checked: true },
        { title: '排序', prop: 'departOrder', width: '', checked: true },
        { title: '状态', prop: 'disabledFlag', width: '', checked: true },
        { title: '备注', prop: 'description', width: '', checked: true },
      ],
      tableData: [],
    }
  },

  mounted() {
    this.loadData()
  },

  methods: {
    onSubmit() {
      this.loadData()
    },
    // 获取分页的数据
    loadData() {
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data //数据
            this.totalCount = res.data.data.total //总条数
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    // 打开添加的对话框
    handleAdd() {
      this.$refs.deptDialog.add()
    },
    // 编辑
    handleEdit(row) {
      this.$refs.deptDialog.edit(row)
    },
    // 打开查看的对话框
    handleShow(row) {
      this.$refs.userDialog.show(row)
    },
    handleDisabled(row) {
      const param = {
        departId: row.departId,
        disabledFlag: row.disabledFlag === 0 ? 1 : 0,
      }
      // disabled(param).then((res) => {
      //   if (res.data.code === 200) {
      //     this.$message.success(res.data.message)
      //     this.loadData()
      //   } else {
      //     this.$message.error(res.data.message)
      //   }
      // })
    },
    // 删除
    handleDelete(row) {
      del(row.departId)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.loadData()
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
  },
}
</script>

<style lang="less" scoped>
.cell {
  display: flex;
}

.demo-form-inline {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
